/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package popups;

/**
 *
 * @author icdev
 */
import Enums.ProjectType;
import PCR.MainFrame;
import components.Band;
import components.Dimer;
import components.HouseKeeping;
import components.Smear;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

public class ToolActionCommander extends MouseAdapter {

    Integer[] pos = {300, 200, 100};
    int count = 0;
    MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
    DefaultTableModel smearsModel;
    DefaultTableModel dimersModel;
    DefaultTableModel bandsModel;
    DefaultTableModel housekeepingsModel;
    JButton b;
    String projectT;

    public void setDimersModel(DefaultTableModel dtm) {
        dimersModel = dtm;
    }

    public void setSmearsModel(DefaultTableModel dtm) {
        smearsModel = dtm;
    }

    public void setBandsModel(DefaultTableModel dtm) {
        bandsModel = dtm;
        
    }

    public void setHouseKeepingsModel(DefaultTableModel dtm) {
        housekeepingsModel = dtm;
    }
    
    String toolTitle;
    

    public void setToolTitle(String str) {
        toolTitle = str;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        try {
            if (e.getClickCount() == 2) {
                JButton curBtn = (JButton) e.getSource();
                if (toolTitle.equals("Smears")) {
                    SmearAction(curBtn);
                } else if (toolTitle.equals("Bands")) {
                    BandAction(curBtn);
                } else if (toolTitle.equals("Dimers")) {
                    DimerAction(curBtn);
                }
                else if (toolTitle.equals("HouseKeepings")) {
                    HouseKeepingAction(curBtn);
                }
    
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /// Write here Any aCtions 
    private void BandAction(JButton PressedBtn) {
        int width = 0, height = 30;
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            width = 120;
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            width = 130;
        } else {
            width = 67;
        }
        String image = (String) bandsModel.getValueAt(Integer.parseInt(PressedBtn.getAccessibleContext().getAccessibleDescription()), 1);
        Band band = new Band();
        if(frm.getSelectedWorkArea().getProjectType()==ProjectType.PRO){
                Icon icn = new javax.swing.ImageIcon("resources/proteinsBand/" + image);
            band.setName("resources/proteinsBand/" + image);
            Image img = ((ImageIcon) icn).getImage();
            ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
            band.setImage(icon);
            band.setSize(width, height);
            band.setVisible(true);}
        else if(frm.getSelectedWorkArea().getProjectType()==ProjectType.PCR || frm.getSelectedWorkArea().getProjectType()==ProjectType.MRNA)
        {
                Icon icn = new javax.swing.ImageIcon("resources/bands/" + image);
            band.setName("resources/bands/" + image);
            Image img = ((ImageIcon) icn).getImage();
            ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
            band.setImage(icon);
            band.setSize(width, height);
            band.setVisible(true);
        }
        
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            band.setLocation(frm.getSelectedWorkArea().get8Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("16")) {
            band.setLocation(frm.getSelectedWorkArea().get16Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            band.setLocation(frm.getSelectedWorkArea().get4Position()[1], 300);
        }


        band.setCurSample(3);
        frm.getSelectedWorkArea().getBandsPanel().add(band);
        frm.getSelectedWorkArea().getBandsPanel().repaint();
        frm.setSelectedShape(band);
        frm.getSelectedWorkArea().setSaved(false);
    }

    private void SmearAction(JButton PressedBtn) {
        int width = 0, height = 30;
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            width = 120;
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            width = 130;
        } else {
            width = 67;
        }
        String image = (String) smearsModel.getValueAt(Integer.parseInt(PressedBtn.getAccessibleContext().getAccessibleDescription()), 1);
        Smear smear = new Smear();
        if(frm.getSelectedWorkArea().getProjectType().toString().equals("PCR")){
        Icon icn = new javax.swing.ImageIcon("resources/smears/" + image);
        smear.setName("resources/smears/" + image);
        Image img = ((ImageIcon) icn).getImage();
        ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        smear.setImage(icon);
        smear.setSize(width, height);
        smear.setVisible(true);}
        else 
            if(frm.getSelectedWorkArea().getProjectType().toString().equals("PRO")){
        Icon icn = new javax.swing.ImageIcon("resources/proteinsmears/" + image);
        smear.setName("resources/proteinsmears/" + image);
        Image img = ((ImageIcon) icn).getImage();
        ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        smear.setImage(icon);
        smear.setSize(width, height);
        smear.setVisible(true);}
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            smear.setLocation(frm.getSelectedWorkArea().get8Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("16")) {
            smear.setLocation(frm.getSelectedWorkArea().get16Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            smear.setLocation(frm.getSelectedWorkArea().get4Position()[1], 300);
        }
        smear.setCurSample(3);
        frm.getSelectedWorkArea().getBandsPanel().add(smear);
        frm.getSelectedWorkArea().getBandsPanel().repaint();
        frm.setSelectedShape(smear);
        frm.getSelectedWorkArea().setSaved(false);
    }

    private void DimerAction(JButton PressedBtn) {
        int width = 0, height = 30;
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            width = 120;
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            width = 130;
        } else {
            width = 67;
        }
        String image = (String) dimersModel.getValueAt(Integer.parseInt(PressedBtn.getAccessibleContext().getAccessibleDescription()), 1);
        Dimer dimer = new Dimer();
        Icon icn = new javax.swing.ImageIcon("resources/dimers/" + image);
        dimer.setName("resources/dimers/" + image);
        Image img = ((ImageIcon) icn).getImage();
        ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        dimer.setImage(icon);
        dimer.setSize(width, height);
        dimer.setVisible(true);
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            dimer.setLocation(frm.getSelectedWorkArea().get8Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("16")) {
            dimer.setLocation(frm.getSelectedWorkArea().get16Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            dimer.setLocation(frm.getSelectedWorkArea().get4Position()[1], 300);
        }
        dimer.setCurSample(3);
        frm.getSelectedWorkArea().getBandsPanel().add(dimer);
        frm.getSelectedWorkArea().getBandsPanel().repaint();
        frm.setSelectedShape(dimer);
        frm.getSelectedWorkArea().setSaved(false);
    }

    private void HouseKeepingAction(JButton PressedBtn) {
        int width = 0, height = 30;
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            width = 120;
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            width = 130;
        } else {
            width = 67;
        }
        String image = (String) housekeepingsModel.getValueAt(Integer.parseInt(PressedBtn.getAccessibleContext().getAccessibleDescription()), 1);
        HouseKeeping housekeeping = new HouseKeeping();
        Icon icn = new javax.swing.ImageIcon("resources/housekeepings/" + image);
        housekeeping.setName("resources/housekeepings/" + image);
        Image img = ((ImageIcon) icn).getImage();
        ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        housekeeping.setImage(icon);
        housekeeping.setSize(width, height);
        housekeeping.setVisible(true);
        if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("8")) {
            housekeeping.setLocation(frm.getSelectedWorkArea().get8Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("16")) {
            housekeeping.setLocation(frm.getSelectedWorkArea().get16Position()[1], 300);
        } else if (frm.getSelectedWorkArea().getBandsPanel().getSampleType().startsWith("4")) {
            housekeeping.setLocation(frm.getSelectedWorkArea().get4Position()[1], 300);
        }
        housekeeping.setCurSample(3);
        frm.getSelectedWorkArea().getBandsPanel().add(housekeeping);
        frm.getSelectedWorkArea().getBandsPanel().repaint();
        frm.setSelectedShape(housekeeping);
        frm.getSelectedWorkArea().setSaved(false);
    }    
}
